package com.mygdx.game.stage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.MainGame;
import com.mygdx.game.group.BottomGroup;
import com.mygdx.game.group.MiddleGroup;
import com.mygdx.game.group.TopGroup;
import com.mygdx.game.resource.Res;
import com.mygdx.game.stage.base.BaseStage;


public class HomeStage extends BaseStage {

    private MainGame mainGame;
    private TopGroup topGroup;
    private MiddleGroup middleGroup;
    private BottomGroup bottomGroup;

    public HomeStage(MainGame game, Viewport viewport) {
        super(game, viewport);
        mainGame = game;
        init();
    }

    public void init() {
        topGroup = new TopGroup(mainGame);
        topGroup.setPosition(getWidth() / 2 - topGroup.getWidth() / 2, getHeight() - topGroup.getHeight() - 20);
        addActor(topGroup);

        middleGroup = new MiddleGroup(mainGame);
        middleGroup.setPosition(10, mainGame.getWorldHeight() / 2 - middleGroup.getHeight() / 2);
        addActor(middleGroup);

        bottomGroup = new BottomGroup(mainGame);
        bottomGroup.setPosition(getWidth() / 2 - bottomGroup.getWidth() / 2, 20);
        addActor(bottomGroup);

        //获取本地最高记录
        Preferences preferences = Gdx.app.getPreferences(Res.BEST_SCORE_PATH);
        topGroup.setBestScore(preferences.getInteger(Res.BEST_SCORE_KEY,0));

        addListener(new HomeInputListener());
    }


    public void reStart() {
        middleGroup.reStart();
        topGroup.reStart();
    }


    public TopGroup getTopGroup() {
        return topGroup;
    }

    private class HomeInputListener extends InputListener {

        //此处用于监听实体按键如在PC平台使用时
        @Override
        public boolean keyDown(InputEvent event, int keyCode) {

            switch (keyCode) {
                case Input.Keys.UP:
                    middleGroup.toUp();
                    break;
                case Input.Keys.DOWN:
                    middleGroup.toDown();
                    break;
                case Input.Keys.LEFT:
                    middleGroup.toLeft();
                    break;
                case Input.Keys.RIGHT:
                    middleGroup.toRight();
                    break;
            }

            return super.keyDown(event, keyCode);
        }

        @Override
        public boolean keyUp(InputEvent event, int keyCode) {
            if (keyCode == Input.Keys.BACK) {
                getMainGame().getGameScreen().setShowExitConfirmStage(true);
                return true;
            }
            return super.keyUp(event, keyCode);
        }
    }

    @Override
    public void dispose(){
        super.dispose();
        //退出前 保存最高记录
        Preferences preferences = Gdx.app.getPreferences(Res.BEST_SCORE_PATH);
        preferences.putInteger(Res.BEST_SCORE_KEY,getTopGroup().getBestScore());
        preferences.flush();
    }
}
